package com.heima.user.service.impl;

import com.heima.common.constants.user.UserRelationConstants;
import com.heima.common.exception.config.CustException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.config.CacheService;
import com.heima.user.service.ApUserRelationService;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class ApUserRelationServiceImpl implements ApUserRelationService {
//    @Autowired
//    RedisTemplate redisTemplate;

    @Autowired
    CacheService cacheService;

    /**
     * app用户关注和取消关注
     *
     * @param userRelationDto
     * @return
     */
    @Override
    public ResponseResult follow(UserRelationDto userRelationDto) {
        //1参数校验
        if (userRelationDto == null || userRelationDto.getAuthorApUserId() == null
                || userRelationDto.getOperation() < 0 || userRelationDto.getOperation() > 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.获取当前登录用户的apUserId
        ApUser apUser = AppThreadLocalUtils.getUser();
        if (apUser == null) {
            CustException.cust("需要登录");
        }
        Integer apUserId = apUser.getId();

        //3.获取当前文章作者的apUserID
        Integer authorApUserId = userRelationDto.getAuthorApUserId();
        //4.关注和取消
        //查询是否已关注
        if (userRelationDto.getOperation() == 0) {
            Double score= cacheService.zScore(UserRelationConstants.FOLLOW_LIST + apUserId, authorApUserId.toString());
//            Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + apUserId, authorApUserId);
            if (score != null) {
                CustException.cust("您已关注，请勿重复点击");
            }
            //关注
            cacheService.zAdd(UserRelationConstants.FOLLOW_LIST + apUserId, authorApUserId.toString(), System.currentTimeMillis());
            cacheService.zAdd(UserRelationConstants.FANS_LIST + authorApUserId, apUserId.toString(), System.currentTimeMillis());

            //redisTemplate.opsForZSet().add(UserRelationConstants.FOLLOW_LIST + apUserId, authorApUserId, System.currentTimeMillis());//写入当前app用户的关注列表中
            //redisTemplate.opsForZSet().add(UserRelationConstants.FANS_LIST + authorApUserId, apUserId, System.currentTimeMillis());//写入当前文章作者app账号的粉丝列表中
        } else {
            //取消关注
            cacheService.zRemove(UserRelationConstants.FOLLOW_LIST + apUserId, authorApUserId.toString());
            cacheService.zRemove(UserRelationConstants.FANS_LIST + authorApUserId, apUserId.toString());

            //redisTemplate.opsForZSet().remove(UserRelationConstants.FOLLOW_LIST + apUserId, authorApUserId);//把当前文章从当前app用户关注列表移除
            //redisTemplate.opsForZSet().remove(UserRelationConstants.FANS_LIST + authorApUserId, apUserId);//把当前app用户从当前文章作者app账号中的粉丝列表移除
        }
        //5.返回结果
        return ResponseResult.okResult();
    }
}
